home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WLINK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-26  |  4.1 KB  |  226 lines

  1. #include <WLink.h>
  2.  
  3. // copyright (c) 1992, 1993 by Paul Wheaton
  4.  
  5. //.parse
  6.  
  7. LinkedList::LinkedList()
  8.   {
  9.     RootRec=NULL;
  10.     CurRec=NULL;
  11.     NumRecs=0;
  12.     CurRecNum=0;
  13.   }
  14.  
  15. //.parse
  16.  
  17. LinkedList::~LinkedList()
  18.   {
  19.     For(CurRecNum,NumRecs)
  20.       {
  21.         LinkedListElement *Temp=CurRec->NextRec;
  22.         #ifdef MAJORBBS
  23.           free(CurRec);
  24.         #else
  25.           delete CurRec;
  26.         #endif
  27.         CurRec=Temp;
  28.       }
  29.   }
  30.  
  31. //.parse
  32.  
  33. Bool LinkedList::Add(void *NewRec)
  34.   {
  35.     #ifdef MAJORBBS
  36.       LinkedListElement* LLE=(LinkedListElement*)malloc(sizeof(LinkedListElement));
  37.     #else
  38.       LinkedListElement* LLE= new LinkedListElement;
  39.     #endif
  40.     if (LLE==NULL) return False;
  41.     LLE->Rec=NewRec;
  42.     if (NumRecs)
  43.       {
  44.         LLE->PrevRec=CurRec;
  45.         LLE->NextRec=CurRec->NextRec;
  46.         CurRec->NextRec->PrevRec=LLE;
  47.         CurRec->NextRec=LLE;
  48.         CurRecNum++;
  49.       }
  50.     else
  51.       {
  52.         RootRec=LLE;
  53.         LLE->NextRec=LLE;
  54.         LLE->PrevRec=LLE;
  55.       }
  56.     CurRec=LLE;
  57.     NumRecs++;
  58.     return True;
  59.   }
  60.  
  61. //.parse
  62.  
  63. Bool LinkedList::Append(void *NewRec)
  64.   {
  65.     if (Size())
  66.       {
  67.         Root();
  68.         Prev();
  69.       }
  70.     return Add(NewRec);
  71.   }
  72.  
  73. //.parse
  74.  
  75. void* LinkedList::operator[](Long Index)
  76.   {
  77.     if (NumRecs==0) return NULL;
  78.     if (NumRecs==1) return(CurRec->Rec);
  79.     Index%=NumRecs;
  80.     if (Index==CurRecNum+1) return(Next());
  81.     if (Index==CurRecNum) return(CurRec->Rec);
  82.     if (Index==0) return(Root());
  83.     if (Index+1==CurRecNum) return(Prev());
  84.     Root();
  85.     Long I=NumRecs/2;
  86.     if (Index>I)
  87.       {
  88.         Long StopVal=NumRecs-Index;
  89.         For(I,StopVal) Prev();
  90.       }
  91.     else For(I,Index) Next(); // search forwards
  92.     return (CurRec->Rec);
  93.   }
  94.  
  95. //.parse
  96.  
  97. void LinkedList::DelCur()
  98.   {
  99.     if (NumRecs>1)
  100.       {
  101.         if (RootRec==CurRec) RootRec=CurRec->NextRec;
  102.         CurRec->NextRec->PrevRec=CurRec->PrevRec;
  103.         CurRec->PrevRec->NextRec=CurRec->NextRec;
  104.         LinkedListElement *Temp=CurRec->NextRec;
  105.         #ifdef MAJORBBS
  106.           free(CurRec);
  107.         #else
  108.           delete CurRec;
  109.         #endif
  110.         CurRec=Temp;
  111.         NumRecs--;
  112.         CurRecNum %= NumRecs;
  113.       }
  114.     else if (NumRecs==1)
  115.       {
  116.         NumRecs=0;
  117.         RootRec=NULL;
  118.         #ifdef MAJORBBS
  119.           free(CurRec);
  120.         #else
  121.           delete CurRec;
  122.         #endif
  123.         CurRec=NULL;
  124.       }
  125.   }
  126.  
  127. //.parse
  128.  
  129. Bool LinkedList::Insert(void *NewRec)
  130.   {
  131.     #ifdef MAJORBBS
  132.       LinkedListElement* LLE=(LinkedListElement*)malloc(sizeof(LinkedListElement));
  133.     #else
  134.       LinkedListElement* LLE= new LinkedListElement;
  135.     #endif
  136.     if (LLE==NULL) return False;
  137.     LLE->Rec=NewRec;
  138.     if (NumRecs)
  139.       {
  140.         LLE->PrevRec=CurRec->PrevRec;
  141.         LLE->NextRec=CurRec;
  142.         CurRec->PrevRec->NextRec=LLE;
  143.         CurRec->PrevRec=LLE;
  144.         if (CurRecNum==0) RootRec=LLE;  // insert before the root
  145.       }
  146.     else
  147.       {
  148.         RootRec=LLE;
  149.         LLE->NextRec=LLE;
  150.         LLE->PrevRec=LLE;
  151.       }
  152.     CurRec=LLE;
  153.     NumRecs++;
  154.     return True;
  155.   }
  156.  
  157. //.parse
  158.  
  159. void* LinkedList::Last()
  160.   {
  161.     if (Size())
  162.       {
  163.         Root();
  164.         return Prev();
  165.       }
  166.     else return NULL;
  167.   }
  168.  
  169. //.parse
  170.  
  171. void *LinkedList::Next()
  172.   {
  173.     if (Size())
  174.       {
  175.         CurRecNum=(CurRecNum+1)%NumRecs;
  176.         CurRec=CurRec->NextRec;
  177.         return CurRec->Rec;
  178.       }
  179.     return NULL;
  180.   }
  181.  
  182. //.parse
  183.  
  184. void* LinkedList::Pop()
  185.   {
  186.     Root();
  187.     void* P=Prev();
  188.     DelCur();
  189.     Prev();
  190.     return P;
  191.   }
  192.  
  193. //.parse
  194.  
  195. void* Queue::Pop()
  196.   {
  197.     void* P=Root();
  198.     DelCur();
  199.     return P;
  200.   }
  201.  
  202. //.parse
  203.  
  204. void *LinkedList::Prev()
  205.   {
  206.     if (Size())
  207.       {
  208.         CurRecNum=(CurRecNum+NumRecs-1)%NumRecs;
  209.           //  trying to avoid a 0-1 situation in an unsigned storage area
  210.         CurRec=CurRec->PrevRec;
  211.         return CurRec->Rec;
  212.       }
  213.     return NULL;
  214.   }
  215.  
  216. //.parse
  217.  
  218. void *LinkedList::Root()
  219.   {
  220.     if (RootRec==NULL) return NULL;
  221.     CurRecNum=0;
  222.     CurRec=RootRec;
  223.     return CurRec->Rec;
  224.   }
  225.  
  226.